package com.book.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.book.dto.ApplyDto;
import com.book.entity.Apply;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ApplyDao extends BaseMapper<Apply> {

    /**
     * 获取某个帖子的所有一级评论(分页)
     * @param postId
     * @return
     */
    @Select("select \n" +
            "   a.id,a.user_id,\n" +
            "   (select head from user where id=a.user_id) as head,\n" +
            "   (select nick_name from user where id=a.user_id) nickName,\n" +
            "   a.post_id as postId, a.content,a.apply_time,\n" +
            "   (select count(*) from apply b where b.parent_apply=a.id) as totalApply\n" +
            "from apply a\n" +
            "where a.post_id=#{postId} and a.parent_id is null and a.del_flag=0\n" +
            "order by a.apply_time desc")
    List<ApplyDto> getFirst(@Param("postId") int postId);


    //某个一级评论的二级评论
    @Select("select  \n" +
            "   a.id,a.user_id, \n" +
            "   (select head from user where id=a.user_id) as head, \n" +
            "   (select nick_name from user where id=a.user_id) nickName, \n" +
            "   a.content,a.apply_time, a.parent_id,\n" +
            "   (select nick_name from user where id=a.parent_id) as parentName,\n" +
            "   a.parent_apply as parentApply\n" +
            "from apply a \n" +
            "where a.post_id=#{postId} and a.parent_apply=#{parentApply} and a.del_flag=0\n" +
            "order by a.apply_time desc")
    List<ApplyDto> getSecond(@Param("postId") int postId, @Param("parentApply") int parentApply);


    //获取自己的所有主动回复
    @Select("select\n" +
            "   a.id,a.user_id as userId,\n" +
            "   (select head from user where id=a.user_id) as head,\n" +
            "   (select nick_name from user where id=a.user_id) as nickName,\n" +
            "   a.post_id,\n" +
            "   (select title from post where id=a.post_id) as postName,\n" +
            "   a.content,a.apply_time as applyTime,\n" +
            "   a.parent_id as parentId,\n" +
            "   (select nick_name from user where id=a.parent_id) as parentName,\n" +
            "   a.parent_apply as parentApply\n" +
            "from apply a\n" +
            "where \n" +
            "   a.user_id=#{userId} and\n" +
            "   (\n" +
            "       (#{level}=1 and a.parent_apply is null) or\n" +
            "       (#{level}=2 and a.parent_apply is not null)\n" +
            "   ) and a.del_flag=0\n" +
            "order by a.apply_time desc")
    List<ApplyDto> getSelfApply(@Param("level") int level, @Param("userId") String userId);

    //获取自己的所欲哦被回复
    @Select("select\n" +
            "   a.id,a.user_id as userId,\n" +
            "   (select head from user where id=a.user_id) as head,\n" +
            "   (select nick_name from user where id=a.user_id) as nickName,\n" +
            "   a.post_id,\n" +
            "   (select title from post where id=a.post_id) as postName,\n" +
            "   a.content,a.apply_time as applyTime,\n" +
            "   a.parent_id as parentId,\n" +
            "   (select nick_name from user where id=a.parent_id) as parentName,\n" +
            "   a.parent_apply as parentApply\n" +
            "from apply a,post b\n" +
            "where \n" +
            "   b.id=a.post_id and\n" +
            "   (\n" +
            "       (#{level}=1 and a.parent_apply is null and b.user_id=#{userId}) or\n" +
            "       (#{level}=2 and a.parent_apply is not null and (a.parent_id=#{userId} or b.user_id=#{userId}))\n" +
            "   ) and a.del_flag=0 and b.del_flag=0\n" +
            "order by a.apply_time desc")
    List<ApplyDto> getApplyMe(@Param("level") int level, @Param("userId") String userId);
}